<?php
uses('Flay');
uses("sanitize");
//$isModuleRender - flag that it's module element render.
if(!isset($isModuleRender))
{
	$isModuleRender = false;
}
$reviewConfig = cr('ReviewConfig');
$presentUrl =  $this->params['controller'].'-'.$this->params['action'];
if($isModuleRender == true)
{
	$presentUrl = "";	
}
$showVotes = true;
$showOtherLinks = true;
$showCustomFields = false;
if(!isset($showAvatar))
{
  $showAvatar = false;
}
if($isModuleRender == false && ($presentUrl == 'reviews-view' || (isset($this->params['pass']['0']) && $this->params['pass']['0'] == 'listing')))
{
	$showAvatar = true;
	$showCustomFields = true;
}
$authedUserId = $session->read('Auth.User.id');
if(!isset($this->Review))
{
	$this->Review = cri('Review');
	$this->Review->setScope('published');
}

//code to set css class name starts here
$themeClass = $view_type;
$containerClass = '';
if($view_type == 'table_view')
{
	$themeClass = 'table_view';
	$containerClass = 'table_view_container';
}
//code to set css class name ends here
?>

<?php
if(empty($reviews))
{
	echo "<h4 class='no_results'>".__("No reviews found!")."</h4>";
}
else
{
	foreach($reviews as $review)
	{
		$reviewClass = "";
		if(isset($review['Review']['is_featured']) && $review['Review']['is_featured'] == 1)
		{
			$reviewClass = "featured";	
		}
		?>
		<!--@todo (1.1) Remove class table_view_container when blog view enabled for review index -->
        <div class="reviewBox <?php echo $reviewClass.' '.$containerClass; ?>" style="">
			<div class="<?php echo $themeClass?>">
				<div class="image">
					<?php echo $html->link($html->image($review['Listing']['image'],array('thumb'=>array('crop'=>true,'width'=>147,'height'=>88))),$review['Listing']['viewUrl'],array('escape'=>false));?>
				</div>
				<?php if($showAvatar){?>
					<div class="poster_info">
					<?php if($review['Review']['poster_entity'] == 'Guest')
						{?>
							<?php echo $html->image($review['Review']['poster']['avatar'],array("thumb"=>array('height'=>"60",'width'=>"60")))?>	
							<?php echo $review['Review']['poster']['display_name'];?>
						
						<?php }else{?>
							<?php echo $html->link($html->image($review['Review']['poster']['avatar'],array("thumb"=>array('height'=>"60",'width'=>"60"))), $review['Review']['poster']['profile_url'], array('escape'=>false,'title'=>$review['Review']['poster']['display_name'],'class' => 'poster')) ?>
							<?php echo $html->link($review['Review']['poster']['display_name'], $review['Review']['poster']['profile_url'], array('class' => 'poster')) ?>
						<?php }?>
				</div>
				<?php } ?>
				<div class="info_review">
					<h5 class="review_title"><?php echo $html->link($review['Review']['title'],$review['Review']['viewUrl']);?></h5>
					<div class="listing_name">
                    	<?php echo __("Posted in")?>
                    	<?php echo $html->link($review['Listing']['title'],$review['Listing']['viewUrl'])?>
                    </div>
                    <div class="show_in_table">
						<h5>
							<?php echo $html->link($review['Review']['title'],$review['Review']['viewUrl'])?>
						</h5>
						<?php echo __("Written By") ?>:
						<?php if($review['Review']['poster_entity'] == 'Guest')
						{?>
							<?php echo $review['Review']['poster']['display_name'];?>
						
						<?php }else{?>
							<?php echo $html->link($review['Review']['poster']['display_name'], $review['Review']['poster']['profile_url'], array('class' => 'poster')) ?>
						<?php }?>
					 </div>	
					 <div class="review_body">
                        <?php
                        $reviewBody = $review['Review']['body'];
						if ($presentUrl != 'reviews-view')
						{
							$reviewBody = strip_tags($review['Review']['body']);
							$reviewBody = str_replace(array("\n",), " ", $reviewBody);
							$reviewBody = Flay::fragment($reviewBody, 120);
						}
						echo $reviewBody;
						?><br/>
						<?php
                        if(!isset($reviewInformation))
                        {
                            $html->initHelper("ReviewInformation");
                            $reviewInformation = $html->ReviewInformation;
                         }
						if ($showCustomFields)
						{
                          echo $reviewInformation->showCustomFields($review);
						}
						?>
					</div>
				</div>
				
				<div class="rating_group">
					<?php
					  $rateableFields = $reviewInformation->getRateableFields($review);
                      echo $reviewInformation->showAverageRating($review,__("Overall rating"));
					?>
						
					<?php
					if ($presentUrl == 'reviews-view' && count($rateableFields) > 1)
					{
						echo $reviewInformation->showDetailedRatings($review);
					}
					?>
					<?php if ($showVotes)
					{?>
						<div class="user_vote"><br />
							<?php echo __("Was this review helpful?") ?>
							<?php echo $vote->yahooStyle($review['Review']['id'], 'Review'); ?>
							<br />
					 	</div>
					<?php
					}?>
				 </div>
				 
				 <div class="clear"></div>
				 <?php if ($showOtherLinks)
				 {?>
					 <div class="review_bottom">
						<?php
						if ($presentUrl != 'reviews-view')
						{
							echo $html->link(__('Read Full'), $review['Review']['viewUrl'], array('class' => 'read_more')) . " ";
							//check if comment on review allowed.
							if ($reviewConfig['allow_comment'])
							{
								echo sprintf(__('Comments (%d)'), $review['Review']['review_comment_count']);
							}?> &nbsp;
							
							<?php echo __("Written By") ?>:
							<?php if($review['Review']['poster_entity'] == 'Guest')
							{?>
								<span class="poster"><?php echo $review['Review']['poster']['display_name'];?></span>&nbsp;
							
							<?php }else{?>
								<?php echo $html->link($review['Review']['poster']['display_name'], $review['Review']['poster']['profile_url'], array('class' => 'poster')) ?>
							<?php }?>
							
							<span class="time_ago">
								<?php echo $time->timeAgoInwords($review['Review']['created'], array('shortString' => true)) ?>
							</span>
							<span style="">
                    				<?php if(cr('ReviewConfig.upload_assets') && isset($review['Review']['listing_asset_count']) && $review['Review']['listing_asset_count'] > 0){?>
									<?php
											$photoText = __("%d photo(s)");
											if($review['Review']['listing_asset_count'] == 1)
											{
												$photoText = __("%d photo");
											}
											echo $html->link('<span class="i-camera"></span>'.sprintf($photoText,$review['Review']['listing_asset_count']),array("controller"=>"listing_assets",'action'=>"index",'listing','listingId'=>$review['Listing']['id'],'userId'=>$review['Review']['user_id']),array('escape'=>false,'class'=>"photo_count"))?>
                    			<?php }?>
							 </span>
						<?php } elseif ($presentUrl == 'reviews-view')
						{ ?>
							<?php
							//check if review edit allowed.
							if ($reviewConfig['allow_review_edit'])
							{
								if ($authedUserId == $review['Review']['user_id'] && !empty($this->params['pass']) && !empty($authedUserId))
								{
								echo $html->link(__('Edit'), array('controller' => 'reviews', 'action' => 'edit', $review['Review']['id']),array('class'=>'edit'));
								}
							}?>	
							<?php echo __("Written By") ?>:
							
							<?php if($review['Review']['poster_entity'] == 'Guest')
							{?>
								<?php echo $review['Review']['poster']['display_name'];?>
							
							<?php }else{?>
								<?php echo $html->link($review['Review']['poster']['display_name'], $review['Review']['poster']['profile_url'], array('class' => 'poster')) ?>
							<?php }?>
							
							<span class="time_ago"><?php echo $time->timeAgoInwords($review['Review']['created'], array('shortString' => true)) ?></span>
							<span style="">
                    				<?php if(cr('ReviewConfig.upload_assets') && isset($review['Review']['listing_asset_count']) && $review['Review']['listing_asset_count'] > 0){?>
									<?php
											$photoText = "%d photo(s)";
											if($review['Review']['listing_asset_count'] == 1)
											{
												$photoText = "%d photo";
											}
											echo $html->link('<span class="i-camera"></span>'.sprintf(__($photoText),$review['Review']['listing_asset_count']),array("controller"=>"listing_assets",'action'=>"index",'listing','listingId'=>$review['Listing']['id'],'userId'=>$review['Review']['user_id']),array('escape'=>false,'class'=>"photo_count"))?>
                    			<?php }?>
							 </span>
							<span class="report">
								<?php echo $userContent->report('Review', $review['Review']['id'], 'Report'); ?>
							</span>
							<span class="views">
								<?php echo $review['Review']['views_count']?> <?php echo __("Views")?>
							</span> 
							
							<?php
						}?>
					</div>
                 <?php
				}?>
			</div>
		</div>
	<?php 
	}//foreach ends here
 }//main if block ends here.
?>
<div class="clear"></div>